
package com.seekting.study;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.LockSupport;

public class HanderThread extends Thread {

    private BlockingQueue<Runnable> blockingQueue;
    private boolean isQuick;

    public HanderThread() {

        blockingQueue = new LinkedBlockingDeque<Runnable>();
        isQuick = true;
    }

    public void put(Runnable run) {
        blockingQueue.add(run);
    }

    @Override
    public void run() {
        while (isQuick) {
            Runnable run;
            try {
                run = blockingQueue.take();
                if (run != null) {
                    run.run();
                } else {
                }
            } catch (InterruptedException e) {
                isQuick = false;
                e.printStackTrace();
            }

        }
    }

    public void quite() {
        System.out.println("quite");
        isQuick = false;
        interrupt();
    }

    public static void main(String[] args) {
        HanderThread handerThread = new HanderThread();
        handerThread.put(new Runnable() {

            @Override
            public void run() {
                byte[] i = new byte[1000000000];
                for (int j = 0; j < i.length; j++) {
                    i[j] = 1;
                }

                System.out.println("gogogo");

            }
        });
        handerThread.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handerThread.quite();

    }
}
